package com.ideal.blockchain.controller.deploy;

import com.ideal.blockchain.deploy.Channel1;
import com.ideal.blockchain.deploy.Org;
import com.ideal.blockchain.deploy.Org1;
import com.ideal.blockchain.dto.response.ResultInfo;
import com.ideal.blockchain.enums.ResponseCodeEnum;
import com.ideal.blockchain.service.deploy.AddOrgServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@RestController
@RequestMapping("/deploy")
public class OrgDeploy {
    @Autowired
    private AddOrgServiceImpl addOrgService;

    List<Org1> existOrg=new ArrayList<>();

    @RequestMapping(value = "/addOrg",method = RequestMethod.POST)
    public ResultInfo addOrg(@RequestBody  Org  org){
        System.out.println("addOrg = " + org.toString());

        for (int i = 0; i < existOrg.size(); i++) {
            if(org.getType().equals("orderer")&&org.getType().equals(existOrg.get(i).getType())){
                return new ResultInfo(ResponseCodeEnum.FAILURE, "只允许一个orderer类型组织");
            }
            if(existOrg.get(i).getName().equals(org.getName())){
                return new ResultInfo(ResponseCodeEnum.FAILURE, "组织名称不能重复");
            }
        }


        Org1 tmpOrg=new Org1();
        tmpOrg.setHosts(StringUtils.join(org.getHost(),","));
        tmpOrg.setName(org.getName());
        tmpOrg.setType(org.getType());
        tmpOrg.setDomain(org.getDomain());
        try {
            addOrgService.addOrg(tmpOrg);
        }catch (Exception e){
            return new ResultInfo(ResponseCodeEnum.FAILURE, e.getMessage());
        }
        return new ResultInfo(ResponseCodeEnum.SUCCESS, "添加组织成功");
    }

    @RequestMapping("/orgList")
    @ResponseBody
    public List<Org> orgList(){
        System.out.println(addOrgService.queryAllOrg().toString());
        List<Org> tmpOrg=new ArrayList<>();
        for(int i=0;i<addOrgService.queryAllOrg().size();i++){
            existOrg.add(addOrgService.queryAllOrg().get(i));
            Org test=new Org();
            test.setName(addOrgService.queryAllOrg().get(i).getName());
            test.setDomain(addOrgService.queryAllOrg().get(i).getDomain());
            test.setType(addOrgService.queryAllOrg().get(i).getType());
            test.setHost(addOrgService.queryAllOrg().get(i).getHosts().split(","));
            tmpOrg.add(test);
        }
        HashSet tmp=new HashSet(existOrg);
        existOrg.clear();
        existOrg.addAll(tmp);
        System.out.println("existOrg="+tmpOrg.toString());
        return tmpOrg;
    }

    @RequestMapping("/orgChannelList")
    @ResponseBody
    public List<Org> orgChannelList(){
        System.out.println(addOrgService.queryAllOrg().toString());
        List<Org> tmpOrg=new ArrayList<>();
        for(int i=0;i<addOrgService.queryAllOrg().size();i++){
            if(addOrgService.queryAllOrg().get(i).getType().equals("orderer")){
                continue;
            }
            Org test=new Org();
            test.setName(addOrgService.queryAllOrg().get(i).getName());
            test.setDomain(addOrgService.queryAllOrg().get(i).getDomain());
            test.setType(addOrgService.queryAllOrg().get(i).getType());
            test.setHost(addOrgService.queryAllOrg().get(i).getHosts().split(","));
            tmpOrg.add(test);
        }
        System.out.println("orgChannelList="+tmpOrg.toString());
        return tmpOrg;
    }

    @RequestMapping(value = "/delOrg",method = RequestMethod.POST)
    public ResultInfo delOrg(@RequestBody Org1 org){
        System.out.println("delOrg = " + org.getName());
        try {
            addOrgService.delOrg(org.getName());
        }catch (Exception e){
            return new ResultInfo(ResponseCodeEnum.FAILURE, e.getMessage());
        }
        return new ResultInfo(ResponseCodeEnum.SUCCESS, "删除组织成功");
    }

//    @RequestMapping(value = "/alterOrg",method = RequestMethod.POST)
//    public String alterOrg(@RequestBody Org org){
//        System.out.println("alterOrg = " + org.toString());
//        Org1 tmpOrg=new Org1();
//        tmpOrg.setHosts(StringUtils.join(org.getHosts(),","));
//        tmpOrg.setName(org.getName());
//        tmpOrg.setType(org.getType());
//        tmpOrg.setDomain(org.getDomain());
//        addOrgService.alterOrg(tmpOrg);
//        return "添加成功";
//    }
}
